package co.fitstart.admin.web.controller.courseware;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.demo2do.core.BusinessException;
import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import co.fitstart.admin.entity.search.MovementSearch;
import co.fitstart.admin.service.MovementService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.movement.Movement;

@Controller
@RequestMapping("/movements")
@MenuSetting("menu-course")
public class MovementController {
	
    private static final Logger logger = LoggerFactory.getLogger(MovementController.class);

    @Autowired
    private MovementService movementService;
    
	/**
	 * 
	 * @param page
	 * @param request
	 * @return
	 */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-movement")
    public ModelAndView list(Page page ,
                             HttpServletRequest request) {
    	
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-list");
        
        List<Movement> movements = movementService.list(page);
        
        modelAndView.addObject("movements", movements);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
               
        return modelAndView;
    }
    
    
    /**
     * 
     * @param online
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@RequestParam("online") boolean online) {
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-search");
        
        modelAndView.addObject("online", online);
        
        return modelAndView;
    }
    
    
    /**
     * 
     * @param movementSearch
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-movement")
    public ModelAndView onSearch(MovementSearch movementSearch,
                                 Page page,
    		                     HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-list");
       
        List<Movement> movements = movementService.search(movementSearch, page);
        
        modelAndView.addObject("movements", movements);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));

        return modelAndView;
    }
    
    /**
     * 
     * @param movementId
     * @return
     */
    @RequestMapping(value = "/{movementId}", method = RequestMethod.GET)
    @MenuSetting("submenu-movement")
    public ModelAndView view(@PathVariable("movementId") Long movementId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-view");
        
        Movement movement= movementService.load(Movement.class, movementId);
        
        modelAndView.addObject("movement",movement);
        
        return modelAndView;
    }
    
    /**
     * 跳转动作数编辑页面
     * @param movementId
     * @return
     */
    @RequestMapping(value = "/{movementId}/movement-number", method = RequestMethod.GET)
    public ModelAndView movementNumber(@PathVariable("movementId") Long movementId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-number");
        
        Movement movement= movementService.load(Movement.class, movementId);
        
        modelAndView.addObject("movement",movement);
        
        return modelAndView;
    }
    
    /**
     * 编辑动作数
     * @param movementId
     * @return
     */
    @RequestMapping(value = "/{movementId}/movement-edit", method = RequestMethod.GET)
    public String  movementEdit(@PathVariable("movementId") Long movementId,
    		                                 @RequestParam(name="movementGroup") int groupAmount,
    		                                 @RequestParam(name="movementNumber") int movementNumber,
    		                                 @RequestParam(name="movementWeight") String maxWeight,
    		                                 @RequestParam(name="movementTime") String time) {
    	
    	logger.info("movementId [{}] movementGroup [{}]  movementNumber [{}] movementWeight [{}] movementTime [{}] ",movementId,groupAmount,movementNumber,maxWeight,time);
    	
        
        movementService.editMovement(groupAmount, movementNumber, maxWeight,movementId,time);
         	 
    
         
        return "redirect:/movements/"+movementId;
    }
    
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create() {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-create");
               
        return modelAndView;
    }
    
    /**
     * 
     * @param movement
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(Movement movement) {
        
        Result result = new Result();
        
        try {
        	
        	movementService.save(movement.initialize());
        	
            result.success().message("创建成功！");
            
        } catch (BusinessException e) {
            
            result.fail().message(e.getMessage());
            
        } catch(Exception e) {
            
            logger.error("#onCreate() - ", e);
            
            result.fail().message("创建失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param movementId
     * @return
     */
    @RequestMapping(value = "/{movementId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("movementId") Long movementId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/movement/movement-delete");
        
        modelAndView.addObject("movementId", movementId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param movement
     * @return
     */
    @RequestMapping(value = "/{movementId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@Preloading("movementId") Movement movement) {
         
        Result result = new Result();
        
        try {
            
            movementService.update(movement.disable());
            
            result.success().message("删除成功！");
            
        } catch(Exception e) {
            
            logger.error("#onDelete() - ", e);
            
            result.fail().message("删除失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
}
